package lol.clann.minecraft.springboot.plugin.core.config.jackson.fill;

import relocate.com.fasterxml.jackson.databind.BeanDescription;
import relocate.com.fasterxml.jackson.databind.DeserializationConfig;
import relocate.com.fasterxml.jackson.databind.deser.ValueInstantiator;
import relocate.com.fasterxml.jackson.databind.module.SimpleValueInstantiators;
import lol.clann.minecraft.springboot.plugin.core.config.jackson.Jackson;

/**
 * @author pyz
 * @date 2018/10/14 上午12:11
 */
public class ThreadlocalSimpleValueInstantiators extends SimpleValueInstantiators {

    @Override
    public ValueInstantiator findValueInstantiator(DeserializationConfig config, BeanDescription beanDesc, ValueInstantiator defaultInstantiator) {
        // 这里必须直接清除ThreadLocal,不然会在使用之前再次走到这里
        Object target = Jackson.getTarget(true);
        if (target != null) {
            return new ThreadLocalValueInstantiator(target);
        }
        return super.findValueInstantiator(config, beanDesc, defaultInstantiator);
    }
}
